package com.gamecollection.minesweeper.controller;

import com.gamecollection.mvc.Controller;
import com.gamecollection.minesweeper.model.MinesweeperModel;
import com.gamecollection.minesweeper.utils.GameUtils;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

/**
 * 扫雷游戏控制器
 */
public class MinesweeperController implements Controller {
    
    private MinesweeperModel model;
    
    public MinesweeperController(MinesweeperModel model) {
        this.model = model;
    }
    
    @Override
    public void initialize() {
        // 控制器初始化
    }
    
    @Override
    public void handleKeyPressed(KeyEvent e) {
        int state = model.getGameState();
        
        switch (e.getKeyCode()) {
            case KeyEvent.VK_ESCAPE:
                // ESC键返回难度选择
                if (state == GameUtils.STATE_PLAYING || 
                    state == GameUtils.STATE_WIN || 
                    state == GameUtils.STATE_GAME_OVER) {
                    model.setGameState(GameUtils.STATE_SELECT_LEVEL);
                }
                break;
            case KeyEvent.VK_R:
                // R键重新开始游戏
                if (state == GameUtils.STATE_PLAYING || 
                    state == GameUtils.STATE_WIN || 
                    state == GameUtils.STATE_GAME_OVER) {
                    model.reset();
                    model.setGameState(GameUtils.STATE_PLAYING);
                }
                break;
        }
    }
    
    @Override
    public void handleKeyReleased(KeyEvent e) {
        // 键盘释放事件处理
    }
    
    @Override
    public void handleMouseClicked(MouseEvent e) {
        int state = model.getGameState();
        
        if (state == GameUtils.STATE_SELECT_LEVEL) {
            handleLevelSelection(e);
        } else if (state == GameUtils.STATE_PLAYING) {
            handleGameClick(e);
        } else if (state == GameUtils.STATE_WIN || state == GameUtils.STATE_GAME_OVER) {
            handleGameOverClick(e);
        }
    }
    
    /**
     * 处理难度选择
     */
    private void handleLevelSelection(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        
        // 简单难度 (100, 150, 300, 60)
        if (x >= 100 && x <= 400 && y >= 150 && y <= 210) {
            model.setLevel(0);
            model.setGameState(GameUtils.STATE_PLAYING);
        }
        // 中等难度 (100, 230, 300, 60)
        else if (x >= 100 && x <= 400 && y >= 230 && y <= 290) {
            model.setLevel(1);
            model.setGameState(GameUtils.STATE_PLAYING);
        }
        // 困难难度 (100, 310, 300, 60)
        else if (x >= 100 && x <= 400 && y >= 310 && y <= 370) {
            model.setLevel(2);
            model.setGameState(GameUtils.STATE_PLAYING);
        }
    }
    
    /**
     * 处理游戏中的点击
     */
    private void handleGameClick(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        
        // 检查是否点击了重新开始按钮
        int centerX = GameUtils.OFFSET + GameUtils.SQUARE_LENGTH * (model.getMapWidth() / 2);
        int centerY = GameUtils.OFFSET;
        
        if (x >= centerX && x <= centerX + GameUtils.SQUARE_LENGTH &&
            y >= centerY && y <= centerY + GameUtils.SQUARE_LENGTH) {
            // 点击了重新开始按钮
            model.reset();
            model.setGameState(GameUtils.STATE_PLAYING);
            return;
        }
        
        // 设置鼠标位置
        model.setMousePosition(x, y);
        
        // 处理鼠标点击
        if (e.getButton() == MouseEvent.BUTTON1) {
            // 左键点击
            model.setLeftClick(true);
        } else if (e.getButton() == MouseEvent.BUTTON3) {
            // 右键点击
            model.setRightClick(true);
        }
    }
    
    /**
     * 处理游戏结束后的点击
     */
    private void handleGameOverClick(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        
        // 检查是否点击了重新开始按钮
        int centerX = GameUtils.OFFSET + GameUtils.SQUARE_LENGTH * (model.getMapWidth() / 2);
        int centerY = GameUtils.OFFSET;
        
        if (x >= centerX && x <= centerX + GameUtils.SQUARE_LENGTH &&
            y >= centerY && y <= centerY + GameUtils.SQUARE_LENGTH) {
            // 点击了重新开始按钮
            model.reset();
            model.setGameState(GameUtils.STATE_PLAYING);
        }
    }
    
    @Override
    public void handleMouseMoved(MouseEvent e) {
        // 鼠标移动事件处理
        model.setMousePosition(e.getX(), e.getY());
    }
    
    @Override
    public void update() {
        // 控制器更新
    }
    
    @Override
    public void startGame() {
        model.setGameState(GameUtils.STATE_PLAYING);
    }
    
    @Override
    public void pauseGame() {
        // 扫雷游戏不支持暂停
    }
    
    @Override
    public void resumeGame() {
        // 扫雷游戏不支持暂停
    }
    
    @Override
    public void endGame() {
        model.setGameState(GameUtils.STATE_GAME_OVER);
    }
} 